package cn.topcodes.tcsf.wechat.service;

import cn.topcode.unicorn.wxsdk.WXContext;
import cn.topcode.unicorn.wxsdk.menu.dto.Button;
import cn.topcode.unicorn.wxsdk.menu.dto.CreateMenu;
import cn.topcode.unicorn.wxsdk.menu.dto.QueryMenuResult;
import cn.topcodes.tcsf.wechat.domain.entity.WechatMenu;
import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信菜单 服务
 * @author unicorn
 * @date 2017-08-15 19:18:38
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class WechatMenuServiceImpl extends GenericService<WechatMenu> implements WechatMenuService {

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void save(Long wechatMpId, CreateMenu createMenu) {
        Example example = this.buildExample();
        example.or().andEqualTo(WechatMenu.FIELD_WECHAT_MP_ID, wechatMpId);
        this.deleteByExample(example);
        List<Button> buttons = createMenu.getButton();
        saveButton(wechatMpId, buttons, null);
    }

    @Override
    public CreateMenu getMenu(Long wechatMpId) {
        Assert.notNull(wechatMpId, "公众号ID不能为空");
        Example example = this.buildExample();
        example.or().andIsNull(WechatMenu.FIELD_PARENT_ID)
                    .andEqualTo(WechatMenu.FIELD_WECHAT_MP_ID, wechatMpId);
        List<WechatMenu> wechatMenus = this.findAll(example);
        CreateMenu createMenu = new CreateMenu();
        List<Button> buttons = wechatMenu2Button(wechatMenus);
        createMenu.setButton(buttons);
        return createMenu;
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    private void saveButton(Long wechatMpId, List<Button> buttons, Long parentId) {
        if(buttons == null || buttons.size() == 0) {
            return;
        }
        for(Button button : buttons) {
            WechatMenu wechatMenu = new WechatMenu();
            wechatMenu.setName(button.getName());
            wechatMenu.setMediaId(button.getMediaId());
            wechatMenu.setMenuKey(button.getKey());
            wechatMenu.setWechatMpId(wechatMpId);
            wechatMenu.setType(button.getType());
            wechatMenu.setUrl(button.getUrl());
            wechatMenu.setParentId(parentId);
            Long id = this.create(wechatMenu);
            if(button.getSubButton() != null && button.getSubButton().size() != 0) {
                saveButton(wechatMpId, button.getSubButton(), id);
            }
        }
    }

    private List<Button> wechatMenu2Button(List<WechatMenu> wechatMenus) {
        if(wechatMenus == null || wechatMenus.size() == 0) {
            return null;
        }
        List<Button> buttons = new ArrayList<>();
        for(WechatMenu wechatMenu : wechatMenus) {
            Button button = new Button();
            button.setName(wechatMenu.getName());
            button.setUrl(wechatMenu.getUrl());
            button.setKey(wechatMenu.getMenuKey());
            button.setType(wechatMenu.getType());
            button.setMediaId(wechatMenu.getMediaId());
            Example example = this.buildExample();
            example.or().andEqualTo(WechatMenu.FIELD_PARENT_ID, wechatMenu.getId())
                        .andEqualTo(WechatMenu.FIELD_WECHAT_MP_ID, wechatMenu.getWechatMpId());
            List<WechatMenu> menus = this.findAll(example);
            button.setSubButton(wechatMenu2Button(menus));
            buttons.add(button);
        }
        return buttons;
    }
}
